<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>把字符串转义为可以在 shell 命令里使用的参数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ref.exec.html">« 程序执行函数</a></li>
      <li style="float: right;"><a href="function.escapeshellcmd.html">escapeshellcmd »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.exec.html">程序执行函数</a></li>
    <li>把字符串转义为可以在 shell 命令里使用的参数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.escapeshellarg" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">escapeshellarg</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.3, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">escapeshellarg</span> &mdash; <span class="dc-title">把字符串转义为可以在 shell 命令里使用的参数</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.escapeshellarg-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>escapeshellarg</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$arg</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>escapeshellarg()</strong></span> 将给字符串增加一个单引号并且能引用或者转义任何已经存在的单引号，这样以确保能够直接将一个字符串传入 shell
   函数，并且还是确保安全的。对于用户输入的部分参数就应该使用这个函数。shell 函数包含<span class="function"><a href="function.exec.html" class="function">exec()</a></span>、<span class="function"><a href="function.system.html" class="function">system()</a></span>
   和<a href="language.operators.execution.html" class="link">执行运算符</a> 。
  </p>
  <p class="para">
   在 Windows 上，<span class="function"><strong>escapeshellarg()</strong></span>
   用空格替换了百分号、感叹号（延迟变量替换）和双引号，并在字符串两边加上双引号。此外，每条连续的反斜线(<code class="literal">\</code>)都会被一个额外的反斜线所转义。
  </p>  
 </div>


 <div class="refsect1 parameters" id="refsect1-function.escapeshellarg-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">arg</code></dt>

     <dd>

      <p class="para">
       需要被转义的参数。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.escapeshellarg-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   转换之后字符串。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.escapeshellarg-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3306">
    <p><strong>示例 #1 <span class="function"><strong>escapeshellarg()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />system</span><span style="color: #007700">(</span><span style="color: #DD0000">'ls&nbsp;'</span><span style="color: #007700">.</span><span style="color: #0000BB">escapeshellarg</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.escapeshellarg-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.escapeshellcmd.html" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - shell 元字符转义</span></li>
    <li class="member"><span class="function"><a href="function.exec.html" class="function" rel="rdfs-seeAlso">exec()</a> - 执行一个外部程序</span></li>
    <li class="member"><span class="function"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
    <li class="member"><span class="function"><a href="function.system.html" class="function" rel="rdfs-seeAlso">system()</a> - 执行外部程序，并且显示输出</span></li>
    <li class="member"><a href="language.operators.execution.html" class="link">执行运算符</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>